﻿from django.shortcuts import render, HttpResponse


# Create your views here.
def index(request):
    '''首页

    准备工作
    '''
    markdown = """# 模板复用和block

## 多层模板复用示例

链接 | 说明
---|---
[index](/3/two) | 首页
[user](/3/user) | user
[item](/3/item) | item

## 单层复用

* 父模板 base.html

    ```html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>{% block title %}{% endblock %}</title>
    </head>
    <body>
    <h1>Hello Dear</h1>
    {% block first %}
    <h2>This is the First!</h1>
    {% endblock %}
    <h2>The End</h2>
    </body>
    </html>
    ```

* 子模板 03.html

    ```html
    {%  extends 'base.html' %}

    {% block title %}我是标题{% endblock %}

    {% block first %}
    <h2>Here you are, Im Now</h2>
    {{ block.super }}
    {% endblock %}
    ```

* 视图

    ```python
    def one(request):
        return render(request, '03.html')
    ```

* 路由

    ```python
    urlpatterns = [
        ...
        path('one', views.one, name='one'),
    ]
    ```

## 多层套用

```bash
              base02_items.html --- items.html
base02.html <                   --- 04_1.html
              base02_users.html --- users.html
```

* 父模板

    ```html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>{% block title %}{% endblock %}-多层</title>
    </head>
    <body>
    <h1>Hello Dear</h1>
    {% block content %}
    <h2>This is the First!</h1>
    {% endblock %}
    <h2>The End</h2>
    </body>
    </html>
    ```

* 二层模板

    * user

        ```html
        {%  extends 'base02.html' %}

        {% block title %}Users{% endblock %}

        {% block content %}
        <h2>Users</h2>
        {% endblock %}
        ```

    * item

        ```html
        {%  extends 'base02.html' %}

        {% block title %}Items{% endblock %}

        {% block content %}
        <h2>Items</h2>
        {% endblock %}
        ```

* 子模板

    * index

        ```html
        {%  extends 'base02.html' %}

        {% block content %}
        <h2>Index</h2>
        {% endblock %}
        ```

    * user

        ```html
        {%  extends 'base02_users.html' %}

        {% block content %}
        <h2>Users content</h2>
        {% endblock %}
        ```

    * item

        ```html
        {%  extends 'base02_items.html' %}

        {% block content %}
        <h2>Items content</h2>
        {% endblock %}
        ```

* 视图

    ```python
    def two(request):
        return render(request, '04_1.html')

    def item(request):
        return render(request, 'items.html')

    def user(request):
        return render(request, 'users.html')
    ```

* 路由

    ```python
    urlpatterns = [
        ...
        path('two', views.two, name='two'),
        path('item', views.item, name='item'),
        path('user', views.user, name='user'),
    ]
    ```

"""
    context = {
        'title': 'Home',
        'markdown': markdown,
    }
    return render(request, 'marked.html', context)


def one(request):
    return render(request, '03.html')


def two(request):
    return render(request, '04_1.html')


def item(request):
    return render(request, 'items.html')


def user(request):
    return render(request, 'users.html')
